In [1]:
%matplotlib inline
from ggplot import *
Making multiple plots at the same time is easy in ggplot. Plots can either be show inline using the print
function, the __repr__
(what python prints out by default for something), or ggplot's save
function.
Since we're trying to make multiple plots, the __repr__
example doesn't really appy here, so we'll be focusing on using print
and save
to make multiple plots.
Let's say you want to generate a histogram of price data for each type of diamond cut in the diamonds
dataset. First thing you need to do is split your dataset up into groups. You can do this using the groupby
function in pandas
.
In [2]:
for i, group in diamonds.groupby("cut"):
print group.head()
Now to create and render a plot for each one of these subgroups, just make a ggplot object add a geom_histogram
layer, and then print the plot.
In [3]:
for name, group in diamonds.groupby("cut"):
p = ggplot(group, aes(x='price')) + geom_histogram() + ggtitle(name)
print(p)
If you want to save the plots to a file instead, just use save
instead of print.
In [4]:
for name, group in diamonds.groupby("cut"):
p = ggplot(group, aes(x='price')) + geom_histogram() + ggtitle(name)
filename = "price-distribution-" + name + ".png"
p.save(filename)
In [ ]: